<?php
namespace app\wechat\controller\api;

use app\wechat\service\WechatService;

/**
 * 处理扫码相关逻辑
 */
class Scan
{

    public static function handle($receive): string
    {

        $result = '收到扫码消息';

        $qrcode_data = WechatService::extractQrcodeData($receive['eventkey']);

        if($qrcode_data['scene_type'] == 'QR_FUNCTION'){
            // 通用的调取内部方法的处理逻辑
            $event_key_data = json_decode($qrcode_data['scene_data'], true);
            try {
                // 用base64对类名及方法进行编码和解码
                $function_arr = explode('@', base64_decode($event_key_data['function']));
                $class = new $function_arr[0]();
                $methods = $function_arr[1];

                $event_key_data['openid'] = $receive['fromusername'];

                $result = $class->$methods($event_key_data);
            } catch (\Exception $e) {
                $result = $e->getMessage();
            }
        }
        return $result;
    }




}